package com.tencent.qcloud.qiaoqiao.ui.fragment;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tencent.qcloud.qiaoqiao.R;
import com.tencent.qcloud.qiaoqiao.login.TCUserMgr;
import com.tencent.qcloud.qiaoqiao.ui.BaseFragment;
import com.tencent.qcloud.qiaoqiao.ui.activity.MainUIActivity;
import com.tencent.qcloud.qiaoqiao.ui.activity.WebViewActivity;
import com.tencent.qcloud.qiaoqiao.ui.tools.utilCode.ActivityUtils;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ChartFragment extends BaseFragment {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ProgressBar)
    android.widget.ProgressBar ProgressBar;
    String url, title;

    public static ChartFragment newInstance() {
        ChartFragment fragment = new ChartFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_webview, container, false);
        ButterKnife.bind(this, view);
        initView();
        loadUrl();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        loadUrl();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    private void loadUrl() {
        ProgressBar.setVisibility(View.GONE);
        if (TCUserMgr.getInstance().hasUser()) {
            return;
        }
        url = "http://mp.conghuaqq.com/index.php?app=bm&user_name="+ TCUserMgr.getInstance().getUserId();
        webView.loadUrl(url);
    }

    private void initView() {
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //  loadUrl();
            }
        });

        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setTextZoom(100);
        // webView.setInitialScale(100);
        WebChromeClient chromeClient = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);

            }
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // 提前加载完成
                if(newProgress>=50){
                    refreshLayout.finishRefresh();
                    ProgressBar.setVisibility(View.GONE);
                }
            }
        };

        // 设置setWebChromeClient对象
        webView.setWebChromeClient(chromeClient);
        webView.setWebViewClient(new  WebViewClientNormal());
        webView.addJavascriptInterface(new JsInterface(), "webkitBridge");
    }


    private class WebViewClientNormal extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // view.loadUrl(url);
            return false;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            //Log.e(TAG, "onReceivedError: ----url:" + error.getDescription());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return;
            }
            //  ToastUtils.showShort(getString(R.string.system_error)+failingUrl);
        }


        @TargetApi(Build.VERSION_CODES.M)
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            //  ToastUtils.showShort(getString(R.string.system_error)+error);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            refreshLayout.finishRefresh();
            ProgressBar.setVisibility(View.GONE);
        }
    }

    public class JsInterface {
        @JavascriptInterface
        public void handleJS(int type) {
            Intent intent = new Intent(mContext,MainUIActivity.class);
            intent.putExtra("type",type);
            ActivityUtils.startActivity(intent);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
